﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL
Imports Boogaloo.Admin.UI.Controls

Namespace UI.UserControls


    Partial Public Class Site_NodeHeader
        Inherits CoreControlBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Groove.admin_current_node IsNot Nothing Then
                If GetEventTargetClicked() <> lb_save_external_url.ClientID Then lbl_message.Visible = False
                If Not Page.IsPostBack Then

                    Dim nodetypes As List(Of NodeTypeLookup) = NodeTypeLookup.GetNodeTypes()
                    If Not Groove.admin_current_node.is_default_node Then
                        rpt_node_type.DataSource = nodetypes
                    Else
                        rpt_node_type.DataSource = GetWebpageNodeTypeList(nodetypes)
                    End If

                    rpt_node_type.DataBind()

                    BindControl()

                End If
            End If

        End Sub


        Private Sub BindControl()
            With Groove.admin_current_node

                lit_node_id.Text = String.Format("<span class=""page node_id""><span>Boogaloo ID: </span>{0}</span>", Groove.admin_current_node.node_id)

                If .node_type = NodeType.Folder Then
                    hyp_preview.Visible = False
                Else
                    hyp_preview.Visible = True
                    If Groove.admin_current_node IsNot Nothing Then
                        If Not Groove.admin_current_node.node_type = NodeType.Folder Then
                            Try
                                hyp_preview.NavigateUrl = Groove.admin_current_node.GetLink().ToString
                            Catch ex As Exception
                                hyp_preview.Visible = False
                            End Try
                        Else
                            hyp_preview.Visible = False
                        End If
                    Else
                        Try
                            hyp_preview.NavigateUrl = Groove.admin_root.default_node.GetLink().ToString
                        Catch ex As Exception
                            hyp_preview.Visible = False
                        End Try
                    End If

                End If


                ' configure the special menu item block
                lbl_menu_item_text.Text = .menu_item_text
                txt_menu_item_text.Text = .menu_item_text
                txt_slug.Text = .slug
                If Not .node_type = NodeType.WebPage Or .is_default_node Then lbl_txt_slug.Visible = False

                hyp_cancel.NavigateUrl = Request.Url.PathAndQuery

                If .menu_item_text = .page_link_text Then
                    lht_page_link_text.label = "<em>Page link text is the same as the menu item text</em>"
                Else
                    lht_page_link_text.label = .page_link_text
                End If
                lht_page_link_text.CommandArgument = "page_link_text"
                lht_page_link_text.Text = .page_link_text

                If String.IsNullOrEmpty(.short_description) Then
                    lht_short_description.label = "(Click to add a short description)"
                Else
                    lht_short_description.label = .short_description
                End If
                lht_short_description.CommandArgument = "short_description"
                lht_short_description.Text = .short_description

                If Groove.admin_current_node.can_be_deleted Then
                    dwiz.Visible = True
                Else
                    dwiz.Visible = False
                End If

                If fld_external.Visible Then
                    txt_external_url.Text = .external_url
                    ddl_link_target.SelectedValue = .external_target
                End If

                If .node_type_id = NodeType.LocalFile Then
                    fld_external.Visible = True
                    lbl_txt_external_url.Text = "Enter the virtual path for the local file"
                    lbl_external_example.Text = "example: /Mindfly/Web/Studio/Contact.pdf"
                ElseIf .node_type_id = NodeType.ExternalUrl Then
                    fld_external.Visible = True
                    lbl_txt_external_url.Text = "Enter the external url"
                    lbl_external_example.Text = "example: www.mindfly.com"
                Else
                    fld_external.Visible = False
                End If

            End With
        End Sub


        Protected Sub rpt_node_type_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_node_type.ItemDataBound

            Dim nt As NodeTypeLookup = CType(e.Item.DataItem, NodeTypeLookup)
            If nt IsNot Nothing Then
                Dim li As HtmlGenericControl = CType(e.Item.FindControl("li"), HtmlGenericControl)
                Dim lb As LinkButton = CType(e.Item.FindControl("lb_node_type"), LinkButton)
                Dim lbl As Label = CType(e.Item.FindControl("lbl_description"), Label)

                If Groove.admin_current_node.node_type_id = nt.node_type_id Then
                    li.Attributes.Add("class", "selected")
                    lb.Enabled = False
                End If

                lb.CommandArgument = nt.node_type_id
                lb.Text = nt.name
                lb.CssClass = nt.name.Replace(" ", "")
                lbl.Text = nt.description

            End If

        End Sub

        Protected Sub lb_node_type_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim n As Node = Groove.admin_current_node

            n.node_type_id = CType(lb.CommandArgument, Integer)
            n.Update()
            Groove.RefreshPage()

        End Sub

        Protected Sub lb_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_save.Click

            If Page.IsValid Then

                Dim n As Node = Groove.admin_current_node

                n.menu_item_text = txt_menu_item_text.Text
                If n.node_type = NodeType.WebPage Then
                    n.slug = txt_slug.Text
                    n.ConstructVirtualPathRewritten(False)
                End If

                n.Update()

                BindControl()

            End If

        End Sub


        Protected Sub lht_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lht_page_link_text.Click, lht_short_description.Click

            If Page.IsValid Then

                Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)
                Dim n As Node = Groove.admin_current_node
                Select Case lht.CommandArgument

                    Case "menu_item_text"
                        n.menu_item_text = lht.Text
                    Case "page_link_text"
                        If String.IsNullOrEmpty(lht.Text) Then
                            n.page_link_text = n.menu_item_text
                        Else
                            n.page_link_text = lht.Text
                        End If
                    Case "short_description"
                        n.short_description = lht.Text

                    Case Else
                        Groove.RefreshPage()
                End Select

                n.Update()
                BindControl()

            End If

        End Sub


        Protected Sub lb_save_external_url_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_save_external_url.Click

            If Page.IsValid Then

                Dim n As Node = Groove.admin_current_node


                n.external_url = txt_external_url.Text
                n.external_target = ddl_link_target.SelectedValue

                n.Update()
                lbl_message.Visible = True
                BindControl()

            End If

        End Sub


        Protected Sub dwiz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles dwiz.Click

            Dim parent_id As Integer = Groove.admin_current_node.parent_id
            If parent_id = 0 Then parent_id = Groove.admin_root.default_node.node_id
            Dim redir As String = String.Format("{0}Designer.aspx?node={1}", Groove.path_admin_site, parent_id)

            Dim success As Boolean = Node.DeleteNode(Groove.admin_current_node.node_id)

            Response.Redirect(redir)


        End Sub


        Private Function GetWebpageNodeTypeList(ByVal nodetypes As List(Of NodeTypeLookup)) As List(Of NodeTypeLookup)

            Dim webpageList As New List(Of NodeTypeLookup)
            Dim found As Boolean = False
            Dim i As Integer = 0
            Do While i < nodetypes.Count And Not found
                If nodetypes(i).node_type_id = NodeType.WebPage Then
                    webpageList.Add(nodetypes(i))
                    found = True
                Else
                    i += 1
                End If
            Loop
            Return webpageList
        End Function

    End Class


End Namespace